layui.define(['table', 'form'], function(exports) {
    var $ = layui.$, table = layui.table, form = layui.form;

    var tradeNo = $('input[name="tradeNo"]').val();

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/market/project/order/operate/logs/list.data'
        ,where:{tradeNo:tradeNo}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'content', title: '内容'}
            ,{field: 'programTypeName', title: '程序'}
            ,{field: 'operatorTypeName', title: '操作者类型'}
            ,{field: 'operator', title: '操作者'}
            ,{field: 'stateName',title: '状态'}
            ,{field: 'errorMsg', title: '错误信息'}
            ,{field: 'createDate', title: '操作时间'}
        ]]
        ,page: false
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    var active = {
        btnSynState:function (){
            layer.confirm('确定要与第三方支付平台同步状态吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/market/project/order/operate/syn.do',
                    data:{tradeNo:tradeNo},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            layer.msg('同步成功',{icon:1},function (){
                                location.reload();
                            });
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        ,btnOrderConfirm: function() {
            layer.confirm('确认后用户将无法再修改收货地址，确定有库存并且物流可达吗？云商品有供应商确定！', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/market/project/order/operate/confirm.do',
                    data:{tradeNo:tradeNo},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            layer.msg('确认订单成功',{icon:1},function (){
                                location.reload();
                            });
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        ,btnLogisticsConfirm: function(){
            layer.open({
                type: 2
                ,title: '填写物流信息'
                ,content: layui.setter.webRoot + '/market/project/order/operate/logistics/add.html?tradeNo='+tradeNo
                ,maxmin: true
                ,area: ['500px', '500px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/market/project/order/operate/logistics/add.do',
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    layer.close(index); //关闭弹层
                                    layer.msg('保存成功',{icon:1},function (){
                                        location.reload();
                                    });
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,btnRefund:function (){
            layer.confirm('确定要进行退款吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/market/project/order/operate/refund.do',
                    data:{tradeNo:tradeNo},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            layer.msg('退款成功',{icon:1},function (){
                                location.reload();
                            });
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    }

    $('.layui-btn.layui-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    exports('goods_order_operate', {})
});